<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <OrgTree class="w-1/5 xl:w-1/6" @select="handleSelect" ref='childTree' />

    <BasicTable @register="registerTable" class="w-4/5 xl:w-5/6" :searchInfo="searchInfo">
      <!-- <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 添加组织 </a-button>
      </template> -->

      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction :actions="[
            {
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                placement: 'left',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]" />
        </template>
      </template>
    </BasicTable>
    <OrgModal @register="registerModal" @success="handleSuccess" />
  </PageWrapper>
</template>

<script lang="ts" setup>
import { provide, reactive, ref } from 'vue'
import { BasicTable, useTable, TableAction } from '@/components/Table'
import { getOrgPageList, deleteOrg } from '@/api/sys/org'
import { PageWrapper } from '@/components/Page'
import { useModal } from '@/components/Modal'
import OrgModal from './OrgModal.vue'
import OrgTree from './OrgTree.vue'
import { columns, searchFormSchema } from './org.data'

defineOptions({ name: 'OrgManagement' })

const childTree = ref<{ fetch: Function }>()
const searchInfo = reactive<Recordable>({})
const [registerModal, { openModal }] = useModal()
const [registerTable, { reload, updateTableDataRecord, setPagination }] = useTable({
  title: '组织列表',
  api: getOrgPageList,
  size: 'small',
  rowKey: 'id',
  columns,
  formConfig: {
    labelWidth: 120,
    schemas: searchFormSchema,
    autoSubmitOnEnter: true,
  },
  striped: true,
  useSearchForm: true,
  showTableSetting: false,
  bordered: true,
  showIndexColumn: true,
  // canResize: false, // 设置该属性，table自使用失效
  actionColumn: {
    width: 150,
    title: '操作',
    dataIndex: 'action',
  },
})

function handleCreate() {
  openModal(true, {
    isUpdate: false,
  })
}

function handleEdit(record: Recordable) {
  openModal(true, {
    record,
    isUpdate: true,
  })
}

async function handleDelete(record: Recordable) {
  await deleteOrg(record.orgId)
  reload();
}

function handleSuccess({ isUpdate, values }) {
  if (isUpdate) {
    // 演示不刷新表格直接更新内部数据。
    // 注意：updateTableDataRecord要求表格的rowKey属性为string并且存在于每一行的record的keys中
    const result = updateTableDataRecord(values.id, values)
    // console.log(result)
  } else {
    reload()
  }
}

function handleSelect(record = '') {
  // console.log(record, '...record');
  searchInfo.parentDeptId = record;
  setPagination({ current: 1 })
  reload()
}

// 父组件调用子组件方法
const callFetchMethodInTree = () => {
  if (childTree.value) {
    childTree.value.fetch();
  }
}

provide('callMethodInTree', callFetchMethodInTree);

</script>
